﻿//Copyright 2014 Jason Bowers
; ko = ko || {};

ko.fromAnnotatedObject = function (obj) {
    for (var p in obj) {
        if (obj.hasOwnProperty(p)) {
            if (!obj[p]) {
                continue;
            }
            var val = obj[p]["jValue"];

            if (typeof obj[p] == "object" && val != "undefined") {
                var rules = obj[p]["jExtend"];
                obj[p] = ko.observable(val);

                for (var r in rules) {
                    var rule = rules[r];

                    if (rule.areSame) {
                        rule.areSame.params = obj[rule.areSame.params];
                    }

                    obj[p].extend(rule);
                };
            } else if (typeof obj[p] == "object") {
                ko.fromAnnotatedObject(obj[p]);
            }
        }
    }

    return obj;
}